#include <QApplication>
#include <QMainWindow>
#include <QAction>
#include <QAction>
#include <QMenuBar>
#include <QWidget>
#include <QToolBar>
#include <QPlainTextEdit>
#include <QFileDialog>
#include <QDockWidget>
#include <QLabel>
#include <QTextEdit>
#include <QStatusBar>
#include <QGridLayout>
#include <QDebug>
//for xml parse (must add "Qt += xml" to .pro file)
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QDomNodeList>
//for stardard c++
#include <iostream>
using namespace std;


//store the words
struct{
  struct {
    QString word, trans, phonetic;
  } words[1000];
  int num;
}dict;

//struct dict_t dict;

int main(int argc, char **argv)
{
  /*
    QApplication app(argc, argv);
  app.setOrganizationName("kang@xmu");
  app.setApplicationName("Qt4 Application Template");
  //app.setWindowIcon();
  //app.setMainWidget(&mw);

  MainWindow mw;
  mw.resize(640,480);
  mw.show();
  mw.setWindowTitle(QApplication::translate("toplevel", "top-level widget"));
  return app.exec();
  */
  QApplication app(argc, argv);
  QLabel *label = new QLabel;


  QDomDocument doc("mydocument");
  QFile file("dict.xml");
  if(!file.open(QIODevice::ReadOnly))
    return 0;
  if(!doc.setContent(&file)){
    file.close();
    return 0;
  }
  file.close();

  //QDomNodeList list = doc.childNodes();
  QDomNodeList list = doc.firstChild().childNodes();	//<item>
  /*
  for(int i=0;i<list.length();i++){
    QDomNode node = list.item(i);
    cout << "node name is " << qPrintable(node.nodeName()) << endl;
  }
  cout << "total: " << list.length() << endl;
  */
  QString word, trans, phonetic;
  dict.num = 0;
  for(int i=0;i<list.length();i++){
    QDomNodeList itemList = list.item(i).childNodes();	//<word>,<trans>,<phonetic>,<tag>
    for(int j=0;j<itemList.length();j++)	//get word,trans,phonetic (ignore tag)
      {
	QDomNode node = itemList.item(j);
	//node.toElement().text();	//get the text of a node
	//node.nodeName()		//node name (QString)
	if(node.nodeName()=="word")
	  word = node.toElement().text();
	else if(node.nodeName()=="trans")
	  trans = node.toElement().text();
	else if(node.nodeName()=="phonetic")
	  phonetic = node.toElement().text();
	//cout << qPrintable(node.nodeName()) << ":" << qPrintable(node.toElement().text()) << endl;
      }
    dict.words[dict.num].word = word;
    dict.words[dict.num].trans = trans;
    dict.words[dict.num].phonetic = phonetic;
    dict.num++;
    
    cout << qPrintable(word) << qPrintable(phonetic) << endl;
    cout << qPrintable(trans) << endl;
  }

  for(int i=0;i<dict.num;i++)
    {
      cout << qPrintable(dict.words[i].word) << endl;
      cout << qPrintable(dict.words[i].phonetic) << endl;
      cout << qPrintable(dict.words[i].trans) << endl;
      sleep(1);
    }





  //print out the element names of all elements that are direct children
  QDomElement docElem = doc.documentElement();
  QDomNode n = docElem.firstChild();
  while(!n.isNull()){
    QDomElement e = n.toElement();	//try to convert the node to an element
    if(!e.isNull()){
      //cout << qPrintable(e.tagName()) << endl;
      //cout << qPrintable(e.text()) << endl;
      label->setText(e.text());
    }
    break;
    n = n.nextSibling();
  }
  
  label->show();
  return app.exec();
  

}
  /*  QString errorStr;
  int errorLine;
  int errorCol;
  */
